import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.SpringLayout;
import javax.swing.SwingConstants;


public class UnlockUserPanel extends JPanel {
	private JTextField txtUsername;

	private JTextField txtAdminUsername;
	
	private JPasswordField adminPassword;
	
	private JButton btnNewButton;
	
	User currentUser;
	private JLabel lblResults;
	
private class buttonListener implements ActionListener{

		
		@Override
		public void actionPerformed(ActionEvent arg0) {
			if(txtAdminUsername.getText().equals(currentUser.username) 
					&& adminPassword.getText().equals(currentUser.password)){
				
				ArrayList<User> result = UserDatabase.searchUser(txtUsername.getText(), UserDatabase.USERNAME_SEARCH);
				User current = null;
				if(!result.isEmpty()){
					current = result.get(0);
				} 
				if(current != null){
					UserDatabase.removeUser(current.username, UserDatabase.DOCTOR);
					UserDatabase.removeUser(current.username, UserDatabase.PATIENT);
					UserDatabase.removeUser(current.username, UserDatabase.SYSTEM_ADMIN);
					UserDatabase.removeUser(current.username, UserDatabase.NURSE);
					current.locked = false;
					
					if(current.usertype.equals("doctor")){
						UserDatabase.addUser(current, UserDatabase.DOCTOR);
					}
					else if(current.usertype.equals("patient")){
						UserDatabase.addUser(current, UserDatabase.PATIENT);
					}
					else if(current.usertype.equals("nurse")){
						UserDatabase.addUser(current, UserDatabase.NURSE);
					}
					else if(current.usertype.equals("admin")){
						UserDatabase.addUser(current, UserDatabase.SYSTEM_ADMIN);
					}
					
				}
				
				DataSaveLoad.save();
				lblResults.setText("Account Unlocked");
			}
			else{
				lblResults.setText("Wrong admin credentials");
			}
			
		}

		
		
		
	}

	public UnlockUserPanel() {
		SpringLayout springLayout = new SpringLayout();
		setLayout(springLayout);
		
		JLabel lblLockUser = new JLabel("Unlock User:");
		springLayout.putConstraint(SpringLayout.NORTH, 
				lblLockUser, 10, SpringLayout.NORTH, this);
		springLayout.putConstraint(SpringLayout.WEST, lblLockUser, 159, SpringLayout.WEST, this);
		springLayout.putConstraint(SpringLayout.EAST, lblLockUser, -159, SpringLayout.EAST, this);
		lblLockUser.setFont(new Font("Tahoma", Font.BOLD | Font.ITALIC, 16));
		add(lblLockUser);
		
		txtUsername = new JTextField();
		add(txtUsername);
		txtUsername.setColumns(10);
		
		JLabel label_1 = new JLabel("System Adminstator Authorization:");
		springLayout.putConstraint(SpringLayout.NORTH, label_1, 26, SpringLayout.SOUTH, txtUsername);
		springLayout.putConstraint(SpringLayout.WEST, 
				label_1, 64, SpringLayout.WEST, this);
		springLayout.putConstraint(SpringLayout.EAST, 
				label_1, -80, SpringLayout.EAST, this);
		label_1.setFont(new Font("Tahoma", Font.BOLD | Font.ITALIC, 16));
		add(label_1);
		
		txtAdminUsername = new JTextField();
		springLayout.putConstraint(SpringLayout.WEST, txtUsername, 0, SpringLayout.WEST, txtAdminUsername);
		springLayout.putConstraint(SpringLayout.NORTH, 
				txtAdminUsername, 32, SpringLayout.SOUTH, label_1);
		springLayout.putConstraint(SpringLayout.WEST, 
				txtAdminUsername, 125, SpringLayout.WEST, this);
		add(txtAdminUsername);
		txtAdminUsername.setColumns(10);
		
		adminPassword = new JPasswordField("");
		springLayout.putConstraint(SpringLayout.NORTH, 
				adminPassword, 19, SpringLayout.SOUTH, txtAdminUsername);
		springLayout.putConstraint(SpringLayout.EAST, 
				txtAdminUsername, 0, SpringLayout.EAST, adminPassword);
		springLayout.putConstraint(SpringLayout.WEST, 
				adminPassword, 125, SpringLayout.WEST, this);
		springLayout.putConstraint(SpringLayout.EAST, 
				adminPassword, 285, SpringLayout.WEST, this);
		add(adminPassword);
		adminPassword.setColumns(10);
		
		btnNewButton = new JButton("Unlock");
		springLayout.putConstraint(SpringLayout.NORTH, 
				btnNewButton, -1, SpringLayout.NORTH, adminPassword);
		springLayout.putConstraint(SpringLayout.EAST, btnNewButton, -30, SpringLayout.EAST, this);
		add(btnNewButton);
		btnNewButton.addActionListener(new buttonListener());
		
		JLabel lblEnterTheUsername = new JLabel("Enter the Username of the User you want to unlock");
		springLayout.putConstraint(SpringLayout.NORTH, txtUsername, 28, SpringLayout.SOUTH, lblEnterTheUsername);
		springLayout.putConstraint(SpringLayout.NORTH, 
				lblEnterTheUsername, 6, SpringLayout.SOUTH, lblLockUser);
		springLayout.putConstraint(SpringLayout.WEST, 
				lblEnterTheUsername, 64, SpringLayout.WEST, this);
		add(lblEnterTheUsername);
		
		JLabel lblUsername = new JLabel("Username:");
		springLayout.putConstraint(SpringLayout.NORTH, lblUsername, 3, SpringLayout.NORTH, txtUsername);
		springLayout.putConstraint(SpringLayout.EAST, lblUsername, -6, SpringLayout.WEST, txtUsername);
		lblUsername.setFont(new Font("Tahoma", Font.BOLD, 11));
		add(lblUsername);
		
		JLabel lblAdminUsername = new JLabel("Admin Username:");
		springLayout.putConstraint(SpringLayout.NORTH, lblAdminUsername, 3, SpringLayout.NORTH, txtAdminUsername);
		springLayout.putConstraint(SpringLayout.EAST, lblAdminUsername, -6, SpringLayout.WEST, txtAdminUsername);
		lblAdminUsername.setFont(new Font("Tahoma", Font.BOLD, 11));
		add(lblAdminUsername);
		
		JLabel lblAdminPassword = new JLabel("Admin Password:");
		springLayout.putConstraint(SpringLayout.NORTH, lblAdminPassword, 3, SpringLayout.NORTH, adminPassword);
		springLayout.putConstraint(SpringLayout.EAST, lblAdminPassword, -6, SpringLayout.WEST, adminPassword);
		lblAdminPassword.setFont(new Font("Tahoma", Font.BOLD, 11));
		add(lblAdminPassword);
		
		lblResults = new JLabel("");
		lblResults.setFont(new Font("Tahoma", Font.ITALIC, 11));
		springLayout.putConstraint(SpringLayout.NORTH, lblResults, 24, SpringLayout.SOUTH, adminPassword);
		springLayout.putConstraint(SpringLayout.WEST, lblResults, 19, SpringLayout.WEST, this);
		springLayout.putConstraint(SpringLayout.EAST, lblResults, -5, SpringLayout.EAST, txtAdminUsername);
		lblResults.setHorizontalAlignment(SwingConstants.CENTER);
		lblResults.setForeground(Color.RED);
		add(lblResults);
	}
	public JButton getBtnNewButton() {
		return btnNewButton;
	}

	public JLabel getLblResult() {
		return lblResults;
	}
	
	public String toString(){
		return "UserUnlocked";
	}
}
